﻿(function ($) {
    oo(':easyui', {
        prop: [
            { name: 'removeMsg', value: '你确定要删除#count#行?', isStatic: true },
            'editIndex',
            {
                name: 'editRow',
                value: '',
                getter: function (owner) {
                    var editIndex = owner.prop('editIndex');
                    return null != editIndex ? owner.call('getRows')[editIndex] : {};
                }
            },
            { name: 'row', value: {} }
        ],
        _datagrid: [
            function (id, op) {
                this.prop('ui', id);
                op.columns = parseColumns(this, op.columns);
                this.prop('options', op).render();
            },
            { remoteSort: false, rownumbers: true, pagination: true, nowrap: false }
        ],
        load: function (module, data) {
            var ajax = oo('ajax').prop('module', module);
            data && ajax.prop('data', data);
            var op = this.call('options');
            op.url = ajax.prop('url');
            this.call('reload');
        },
        appendNewRow: function () {
            this.appendRow(this.prop('row'));
        },
        appendRow: function (row) {
            this.call('acceptChanges');
            this.call('appendRow', row);
            this.beginEditor(this.call('getRows').length - 1);
        },
        beginEdit: function (editIndex) {
            this.prop('editIndex', editIndex).call('selectRow', editIndex);
            this.call('beginEdit', editIndex);
        },
        removeSelections: function (callback) {
            this.editSelections(function (rows) {
                var count = rows.count;
                if (count) {
                    $.messager.confirm('', this.prop('removeMsg').replace('#count#', count), (function (self) {
                        return function (b) {
                            if (b) {
                                $.each(rows, function () {
                                    self.call('deleteRow', self.call('getRowIndex', this));
                                });
                                callback ? callback(rows) : self.call('loaded');
                            }
                            else {
                                self.call('loaded');
                            }
                        };
                    })(this));
                }
                else {
                    this.call('loaded');
                }
            });
        },
        editSelections: function (callback) {
            this.call('acceptChanges');
            var rows = this.call('getSelections');
            this.call('loading');
            callback.call(this, rows);
        },
        editPageRows: function (callback) {
            this.call('acceptChanges');
            this.call('loading');
            callback.call(this, this.call('getRows'));
        },
        displayColumns: function (fields, display) {
            var method = display ? 'showColumn' : 'hideColumn';
            for (var i = 0, field; field = fields[i]; i++) {
                this.call(method, field);
            }
        }
    });

    function parseColumns(gv, arrColumns) {
        var newArrColumns = [];
        $.each(arrColumns, function () {
            var newColumns = [], columnNames = {};
            $.each(this, function () {
                if (this.hasOwnProperty('editor') && this.editor.type == 'combobox') {
                    if (!columnNames.hasOwnProperty(this.keyField)) {
                        columnNames[this.keyFiled] = 1;
                        newColumns.push({ field: this.keyField, hidden: true });
                    }
                    var newColumn = createComboboxColumn(gv, this);
                    newColumns.push(newColumn);
                }
                else {
                    newColumns.push(this);
                }
            });
            newArrColumns.push(newColumns);
        });
        return newArrColumns;
    };

    //column = { keyField: 'id', valueField: 'name', title: '测试', editor: { type: 'combobox', options: { url: 地址' }} };
    function createComboboxColumn(gv, column) {
        var combobox = oo('combobox');
        var op = combobox.prop('options', column.editor.options).prop('options');
        op.valueField = op.textField;
        op.onSelect || (op.onSelect = (function (field) {
            return function (data) {
                gv.prop('editRow')[field] = data.key;
            };
        })(column.keyField));
        var newColumn = $.extend(true, {}, column, { field: column.valueField, editor: { options: op} });
        delete newColumn.keyField;
        delete newColumn.valueField;
        return newColumn;
    };
})(jQuery);